//#include <stdio.h>
//
//int main() {
//    int a;
//    printf("请输入一个数字 a（0-9）：");
//    scanf_s("%d", &a);
//
//
//    int term1 = a;
//    int term2 = a * 10 + a;
//    int term3 = term2 * 10 + a;
//    int term4 = term3 * 10 + a;
//    int term5 = term4 * 10 + a;
//
//    
//    int sum = term1 + term2 + term3 + term4 + term5;
//
//    
//    printf("前5项之和为： %d\n", sum);
//
//    return 0;
//}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // 用于清屏

#define ROWS 9       // 行数
#define COLS 9       // 列数
#define MINES 10     // 地雷数量
#define MAX_INPUT 16 // 输入缓冲区大小

// 游戏状态枚举
enum GameState {
    PLAYING,
    WIN,
    LOSE
};

// 全局游戏数据
int mineBoard[ROWS][COLS];  // 地雷分布（-1=雷，0-8=周围雷数）
int gameBoard[ROWS][COLS];  // 玩家棋盘（-2=未打开，-1=标记，0-8=显示数字）
int remaining;              // 剩余安全格子
enum GameState gameState;   // 当前游戏状态

// 初始化游戏
void InitializeGame() {
    remaining = ROWS * COLS - MINES;
    gameState = PLAYING;

    // 初始化棋盘
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            mineBoard[i][j] = 0;
            gameBoard[i][j] = -2; // -2表示未打开
        }
    }

    // 随机布雷
    srand((unsigned)time(NULL));
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int x = rand() % ROWS;
        int y = rand() % COLS;
        if (mineBoard[x][y] != -1) {
            mineBoard[x][y] = -1;
            minesPlaced++;
        }
    }

    // 计算数字提示
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (mineBoard[i][j] == -1) continue;

            int count = 0;
            for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
                    int nx = i + dx;
                    int ny = j + dy;
                    if (nx >= 0 && nx < ROWS && ny >= 0 && ny < COLS) {
                        if (mineBoard[nx][ny] == -1) count++;
                    }
                }
            }
            mineBoard[i][j] = count;
        }
    }
}

// 打印游戏界面
void PrintInterface() {
    system("cls");
    printf("   扫雷游戏（剩余雷数：%d）\n", MINES);

    // 打印列号
    printf("   ");
    for (int j = 0; j < COLS; j++) {
        printf("%2d ", j + 1);
    }
    printf("\n   ");
    for (int j = 0; j < COLS; j++) {
        printf("---");
    }
    printf("\n");

    // 打印棋盘
    for (int i = 0; i < ROWS; i++) {
        printf("%2d|", i + 1); // 行号
        for (int j = 0; j < COLS; j++) {
            switch (gameBoard[i][j]) {
            case -2: printf(" * "); break; // 未打开
            case -1: printf(" @ "); break; // 标记雷
            default: printf("%2d ", gameBoard[i][j]); // 显示数字
            }
        }
        printf("\n");
    }
}

// 递归展开空白区域
void ExpandArea(int x, int y) {
    if (x < 0 || x >= ROWS || y < 0 || y >= COLS) return;
    if (gameBoard[x][y] != -2) return;

    gameBoard[x][y] = mineBoard[x][y];
    remaining--;

    if (mineBoard[x][y] == 0) {
        // 展开周围8个方向
        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
                if (dx != 0 || dy != 0) {
                    ExpandArea(x + dx, y + dy);
                }
            }
        }
    }
}

// 处理玩家输入
void ProcessInput() {
    char input[MAX_INPUT];
    printf("\n操作说明：\n");
    printf("打开格子：行号 列号（例：3 5）\n");
    printf("标记地雷：m 行号 列号（例：m 2 4）\n");
    printf("请输入操作：");

    fgets(input, MAX_INPUT, stdin);

    int x, y;
    if (sscanf_s(input, "m %d %d", &x, &y) == 2) {
        // 标记地雷
        x--; y--;
        if (x >= 0 && x < ROWS && y >= 0 && y < COLS) {
            if (gameBoard[x][y] == -2) {
                gameBoard[x][y] = -1;
            }
            else if (gameBoard[x][y] == -1) {
                gameBoard[x][y] = -2;
            }
        }
    }
    else if (sscanf_s(input, "%d %d", &x, &y) == 2) {
        // 打开格子
        x--; y--;
        if (x >= 0 && x < ROWS && y >= 0 && y < COLS) {
            if (mineBoard[x][y] == -1) {
                gameState = LOSE;
                return;
            }
            if (gameBoard[x][y] == -2) {
                ExpandArea(x, y);
                if (remaining == 0) gameState = WIN;
            }
        }
    }
}

int main() {
    InitializeGame();

    while (gameState == PLAYING) {
        PrintInterface();
        ProcessInput();
    }

    // 游戏结束处理
    PrintInterface();
    if (gameState == WIN) {
        printf("\n恭喜你，扫雷成功！\n");
    }
    else {
        printf("\n很遗憾，你踩到地雷了！\n");
        // 显示所有地雷位置
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                if (mineBoard[i][j] == -1) gameBoard[i][j] = -3;
            }
        }
        PrintInterface();
    }

    system("pause");
    return 0;
}